#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

float distance(char latCusAngle,char lonCusAngle,char latResAngle,char lonResAngle) 
{
	char latCusAngle[20],lonCusAngle[20],latResAngle[20],lonResAngle[20];
	char ch1[20],ch2[20],ch3[20],ch4[20];
	float latCustomer,lonCustomer,latRestaurant,lonRestaurant;
    float pai=3.1415926;
    int longer1,longer2,longer3,longer4;
    longer1 = strlen(latCusAngle);
    longer2 = strlen(lonCusAngle);
    longer3 = strlen(latResAngle);
    longer4 = strlen(lonResAngle);
    int i;
    float num1,num2,num3,num4;
    for(i = 1 ; i < longer1 ; i++)
	    {
		ch1[i-1]=latCusAngle[i];
		}
		num1=atof(ch1);
	for(i = 1 ; i < longer2 ; i++)
	    {
		ch2[i-1]=lonCusAngle[i];
		}
		num2=atof(ch2);
	for(i = 1 ; i < longer3 ; i++)
	    {
		ch3[i-1]=latResAngle[i];
		}
		num3=atof(ch3);
	for(i = 1 ; i < longer4 ; i++)
	    {
		ch4[i-1]=lonResAngle[i];
		}
		num4=atof(ch4);
    if (latCusAngle[0]=='N')
       {
	   latCustomer=pai*num1/180;
       } 
       else
	   {
	   latCustomer=-pai*num1/180;
       } 
	   
	if (lonCusAngle[0]=='E')
	   {
	   lonCustomer=pai*num2/180;
       }   
	   else
	   {
		lonCustomer=-pai*num2/180;
	   }
	if (latResAngle[0]=='N')
	    {
	 	latRestaurant=pai*num3/180;
	    }
	    else
		{
		 latRestaurant=-pai*num3/180;
	    }   
	if  (lonResAngle[0]=='E' )
	    {
		lonRestaurant=pai*num4/180;
	    }
		else
	    {
		lonRestaurant=-pai*num4/180;
	    }  
	int a=6378137;
	//地球长半轴 .
	int b=6356755;
	//地球短半轴 
	double c,e,dCus,NCus,fCus,mueCus,lambdaCus,xCus,yCus,dRes,NRes,fRes,mueRes,lambdaRes,xRes,yRes,distance;
	c=pow(a,2)-pow(b,2);
	e=sqrt(c)/a;
	//椭圆第一偏心率 
	dCus=1-pow(e*sin(latCustomer),2);
	NCus=a/sqrt(dCus);
	//客户点卯酉圈曲率半径
	fCus=NCus*(1-pow(e,2));
	mueCus=fCus/dCus;
	//客户点子午圈曲率半径
	lambdaCus=NCus*cos(latCustomer);
	//客户点平行圈曲率半径
	xCus=mueCus*lonCustomer;
	yCus=lambdaCus*latCustomer; 
	dRes=1-pow(e*sin(latRestaurant),2);
	NRes=a/sqrt(dRes);
	//商家点卯酉圈曲率半径
	fRes=NRes*(1-pow(e,2));
	mueRes=fRes/dRes;
	//商家点子午圈曲率半径
	lambdaRes=NRes*cos(latRestaurant);
	//商家点平行圈曲率半径
	xRes=mueRes*lonRestaurant;
	yRes=lambdaRes*latRestaurant; 
	distance=sqrt(pow(xCus-xRes,2)-pow(yCus-yRes,2));
	return distance;
}
